﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TST.SharePointObjects
{
    public class SiteObjectStore
    {
        private Guid _site;
        private Guid _web;
        private Guid _farmId;
        private Guid _webApplicationId;
        private SharePointObjectsDatabase _database;

        public SiteObjectStore(Guid farmID, Guid webApplicationID, Guid siteID, Guid webID, SharePointObjectsDatabase database)
        {
            _site = siteID;
            _web = webID;
            _farmId = farmID;
            _webApplicationId = webApplicationID;
            _database = database;
        }

        public DataTable GetSiteObjects(SiteObjectType objectType)
        {
            return _database.GetData(GetSelectProcedure(objectType), CommandType.StoredProcedure);
        }

        internal void UpdateSiteObject(SiteObject siteObject)
        {
            if (siteObject == null)
                return;
            _database.RunStoredProcedure(GetUpdateProcedure(siteObject));
        }

        public DataTable GetIndexObjects()
        {
            return _database.GetData(GetIndexObjectsProcedure(), CommandType.StoredProcedure);
            //SqlCommand selectCommand = new SqlCommand();
            //selectCommand.CommandType = CommandType.Text;
            //selectCommand.CommandText = "select SiteID, WebID, ObjectID, Name, ObjectType, [Index], Url, IndexScope, IndexByProperty, IndexItems, IndexId from SiteObjects where [Index]=1";
            //return Database.GetData(selectCommand, CommandType.Text);
        }

        private SqlCommand GetUpdateProcedure(SiteObject siteObject)
        {
            // Setup the UPDATE command
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.CommandText = "UpdateSiteObjects";

            SqlParameter paramFarmID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramFarmID.Value = _farmId;
            updateCommand.Parameters.Add(paramFarmID);

            SqlParameter paramWebApplicationID = new SqlParameter("@WebApplicationID", SqlDbType.UniqueIdentifier);
            paramWebApplicationID.Value = _webApplicationId;
            updateCommand.Parameters.Add(paramWebApplicationID);

            SqlParameter paramSiteID = new SqlParameter("@SiteID", SqlDbType.UniqueIdentifier);
            paramSiteID.Value = _site;
            updateCommand.Parameters.Add(paramSiteID);

            SqlParameter paramWebID = new SqlParameter("@WebID", SqlDbType.UniqueIdentifier);
            paramWebID.Value = _web;
            updateCommand.Parameters.Add(paramWebID);

            SqlParameter paramName = new SqlParameter("@Name", SqlDbType.NVarChar, 255);
            paramName.Value = siteObject.Name;
            updateCommand.Parameters.Add(paramName);

            SqlParameter paramObjectID = new SqlParameter("@ObjectID", SqlDbType.NVarChar, 512);
            paramObjectID.Value = siteObject.Id;
            updateCommand.Parameters.Add(paramObjectID);

            SqlParameter paramObjectType = new SqlParameter("@ObjectType", SqlDbType.TinyInt);
            paramObjectType.Value = (int)siteObject.SiteObjectType;
            updateCommand.Parameters.Add(paramObjectType);

            SqlParameter paramUrl = new SqlParameter("@Url", SqlDbType.NVarChar, 255);
            if (string.IsNullOrEmpty(siteObject.Url))
                paramUrl.Value = System.DBNull.Value;
            else
                paramUrl.Value = siteObject.Url;
            updateCommand.Parameters.Add(paramUrl);

            SqlParameter paramIndex = new SqlParameter("@Index", SqlDbType.Bit);
            paramIndex.Value = siteObject.Index;
            updateCommand.Parameters.Add(paramIndex);

            SqlParameter paramIndexScope = new SqlParameter("@IndexScope", SqlDbType.TinyInt);
            paramIndexScope.Value = (int)siteObject.IndexScope;
            updateCommand.Parameters.Add(paramIndexScope);

            SqlParameter paramIndexByProperty = new SqlParameter("@IndexByProperty", SqlDbType.TinyInt);
            paramIndexByProperty.Value = (int)siteObject.IndexByProperty;
            updateCommand.Parameters.Add(paramIndexByProperty);

            SqlParameter paramItems = new SqlParameter("@IndexItems", SqlDbType.Bit);
            paramItems.Value = siteObject.IndexItems;
            updateCommand.Parameters.Add(paramItems);

            SqlParameter paramIndexId = new SqlParameter("@IndexId", SqlDbType.NVarChar, 512);
            paramIndexId.Value = siteObject.IndexId;
            updateCommand.Parameters.Add(paramIndexId);

            return updateCommand;
        }

        private SqlCommand GetDeleteCommand()
        {
            // Setup the DELETE command
            SqlCommand deleteCommand = new SqlCommand();
            deleteCommand.CommandType = CommandType.Text;
            deleteCommand.UpdatedRowSource = UpdateRowSource.None;
            deleteCommand.CommandText = string.Format("delete from SiteObjects where SiteID='{0}' and WebID='{1}' and ObjectType=@ObjectType and ObjectID=@ObjectID", _site, _web);

            SqlParameter paramObjectID = new SqlParameter("@ObjectID", SqlDbType.NVarChar, 512);
            paramObjectID.SourceColumn = "ObjectID";
            deleteCommand.Parameters.Add(paramObjectID);

            SqlParameter paramObjectType = new SqlParameter("@ObjectType", SqlDbType.TinyInt);
            paramObjectType.SourceColumn = "ObjectType";
            deleteCommand.Parameters.Add(paramObjectType);

            return deleteCommand;
        }

        private SqlCommand GetSelectProcedure(SiteObjectType objectType)
        {
            SqlCommand selectCommand = new SqlCommand();
            selectCommand.CommandText = "GetSiteObjects";

            SqlParameter paramFarmID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramFarmID.Value = _farmId;
            selectCommand.Parameters.Add(paramFarmID);

            SqlParameter paramSiteID = new SqlParameter("@SiteID", SqlDbType.UniqueIdentifier);
            paramSiteID.Value = _site;
            selectCommand.Parameters.Add(paramSiteID);

            SqlParameter paramWebID = new SqlParameter("@WebID", SqlDbType.UniqueIdentifier);
            paramWebID.Value = _web;
            selectCommand.Parameters.Add(paramWebID);

            SqlParameter paramObjectType = new SqlParameter("@ObjectType", SqlDbType.TinyInt);
            paramObjectType.Value = (int)objectType;
            selectCommand.Parameters.Add(paramObjectType);

            return selectCommand;
        }

        private SqlCommand GetIndexObjectsProcedure()
        {
            SqlCommand selectCommand = new SqlCommand();
            selectCommand.CommandText = "GetIndexObjects";

            SqlParameter paramFarmID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramFarmID.Value = _farmId;
            selectCommand.Parameters.Add(paramFarmID);

            return selectCommand;
        }

        internal void ReadDataRow(System.Data.DataRow row, SiteObject siteObject)
        {
            siteObject.FarmId = (Guid)row["FarmId"];
            siteObject.WebApplicationId = (Guid)row["WebApplicationId"];
            siteObject.SiteId = (Guid)row["SiteId"];
            siteObject.WebId = (Guid)row["WebId"];
            siteObject.Id = (string)row["ObjectId"];
            siteObject.Name = (string)row["Name"];
            siteObject.SiteObjectType = (SiteObjectType)Enum.Parse(typeof(SiteObjectType), row["ObjectType"].ToString());
            if (row["Url"].GetType() != typeof(System.DBNull))
            {
                siteObject.Url = (string)row["Url"];
            }
            siteObject.Index = (bool)row["Index"];
            siteObject.IndexItems = (bool)row["IndexItems"];
            siteObject.IndexScope = (ObjectReferenceScope)Enum.Parse(typeof(ObjectReferenceScope), row["IndexScope"].ToString());
            siteObject.IndexByProperty = (IndexByProperty)Enum.Parse(typeof(IndexByProperty), row["IndexByProperty"].ToString());
            siteObject.IndexId = (string)row["IndexId"];
        }

    }

    public enum SiteObjectType
    {
        All = 0,
        ContentType = 1,
        SiteColumn = 2,
        SharePointGroup = 3,
        FeatureDefinition = 4
    }
}
